package blog.service.impl;

import blog.dao.SupportDao;
import blog.pojo.Support;
import blog.service.SupportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupportServiceImpl implements SupportService {

    @Autowired
    private SupportDao supportDao;

    @Override
    public Page<Support> getPage(Page page, Support support, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<Support> queryWrapper = new QueryWrapper<>(support)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.like("name", keywords);
        }
        return supportDao.selectPage(page, queryWrapper);
    }

    @Override
    public Support getInfo(Integer id) throws Exception {
        return supportDao.selectOne(new QueryWrapper<Support>().setEntity(new Support().setId(id)));
    }

    @Override
    public Boolean update(Support support) throws Exception {
        return supportDao.updateById(support) > 0;
    }

    @Override
    public Boolean insert(Support support) throws Exception {
       return supportDao.insert(support) > 0;
    }

    @Override
    public List<Support> selectAll() throws Exception {
        return supportDao.selectList(new QueryWrapper<>(new Support().setIsuse(1)));
    }
}
